home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0041_Delete Tree for DELPHI.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  1.5 KB  |  55 lines

  1. {
  2. >I still need some help writing a program similar to MS-DOS DELTREE.
  3. >Even though I RTFM, I aparrently don't understand the syntax for
  4. >FindFirst and FindNext.
  5.  
  6. I was just playing with this yesterday.  The following is not pretty,
  7. but it should work.  Hope it helps.
  8. }
  9. procedure DelTree(const RootDir  : String);
  10. var
  11.   SearchRec : TSearchRec;
  12. begin
  13.  
  14. Try
  15.  
  16.     ChDir(RootDir);  {Path to the directory  given as parameter }
  17.  
  18.     FindFirst('*.*',faAnyFile,SearchRec);
  19.  
  20.     Erc := 0;
  21.     while Erc = 0 do  begin
  22.  
  23.         { Ignore higher level markers }
  24.         if      ((SearchRec.Name <> '.' )
  25.         and  (SearchRec.Name <> '..')) then begin
  26.  
  27.               if  (SearchRec.Attr and faDirectory>0) then begin
  28.                     { Have found a directory, not a file.
  29.                        Recusively call ouselves to delete its files }
  30.                      DelTree(SearchRec.Name);
  31.                      end
  32.               else begin
  33.                     {Found a file.  Delete it or whatever
  34.                      you want to do here }
  35.                      end;
  36.          end;
  37.  
  38.           Erc := FindNext (SearchRec);
  39.           { Erc is zero if FindNext successful,
  40.             otherwise Erc = negative DOS error }
  41.  
  42.            {Give someone else a chance to run}
  43.             Application.ProcessMessages;
  44.  
  45.     end;
  46.  
  47. finally
  48.       { If we are not at the root of the disk, back up a level }
  49.       if Length(RootDir) > 3 then
  50.           ChDir('..');
  51.       { I guess you would remove directory RootDir here }
  52. end;
  53.  
  54. end;
  55.